/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import bean.Table;
import client.DatabaseHandler;
import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kristoffer
 */
public class Controller extends HttpServlet{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1648322956489292305L;
	private DatabaseHandler dbhand = null;
    private String host = "localhost";

    public Controller(){
    }

    private void connectToRMI(){
        try {
         Registry registry = LocateRegistry.getRegistry(host, 1234);
		 dbhand = (DatabaseHandler) registry.lookup("dbImpl");
      } catch (RemoteException ex) {
         System.err.println("RemoteException");
         ex.printStackTrace();
      } catch (NotBoundException ex) {
         System.err.println("NotBoundException");
         ex.printStackTrace();
      }
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response){
        connectToRMI();
        System.err.println("----doGet()----");
        HttpSession session = request.getSession();
        ServletContext sc = getServletContext();

        java.util.Enumeration<String> enu = request.getParameterNames();
        System.err.println("Parameters:");
        while(enu.hasMoreElements()){
            System.err.println(" " + enu.nextElement());
        }

        if(request.getParameter("action") != null){
            if(request.getParameter("action").equals("add_table")){
                showAddTablePage(request, response, session, sc);
            }
            else if(request.getParameter("action").equals("show_table")){
                showShowTablePage(request, response, session, sc);
            }
            else if(request.getParameter("action").equals("add_data")){
                
            }
        }
        else if(request.getParameter("handle_data") != null){
            if(request.getParameter("handle_data").equals("add_table_1")){
                showAddTablePage2(request, response, session, sc);
            }
            else if(request.getParameter("handle_data").equals("add_table_2")){
                showAddTablePage3(request, response, session, sc);
            }
            else if(request.getParameter("handle_data").equals("show_table_1")){
                showShowTablePage2(request, response, session, sc);
            }
        }
        else{
            showDefaultPage(request, response, session, sc);
        }
    }

    private void showDefaultPage(HttpServletRequest request, HttpServletResponse response, HttpSession session, ServletContext sc){
        System.err.println("----showDefaultPage()----");
        RequestDispatcher rd = sc.getRequestDispatcher("/main.jsp");
        try{
            rd.forward(request, response);
        }
        catch(ServletException e){
            System.err.println(e.getMessage());
        }
        catch(IOException e){
            System.err.println(e.getMessage());
        }
    }

    private void showAddTablePage(HttpServletRequest request, HttpServletResponse response, HttpSession session, ServletContext sc){
        System.err.println("----showAddTablePage()----");
        RequestDispatcher rd = sc.getRequestDispatcher("/add_table.jsp");
        try{
            rd.forward(request, response);
        }
        catch(ServletException e){
            System.err.println(e.getMessage());
        }
        catch(IOException e){
            System.err.println(e.getMessage());
        }
    }

    private void showAddTablePage2(HttpServletRequest request, HttpServletResponse response, HttpSession session, ServletContext sc){
        System.err.println("----showAddTablePage2()----");

        String tablename = request.getParameter("tablename");
        int colcount = Integer.parseInt(request.getParameter("colcount"));

        Table table = new Table();
        table.setColcount(colcount);
        table.setTablename(tablename);
        session.setAttribute("table", table);

        RequestDispatcher rd = sc.getRequestDispatcher("/add_table2.jsp");
        try{
            rd.forward(request, response);
        }
        catch(ServletException e){
            System.err.println(e.getMessage());
        }
        catch(IOException e){
            System.err.println(e.getMessage());
        }
    }

    private void showAddTablePage3(HttpServletRequest request, HttpServletResponse response, HttpSession session, ServletContext sc){
        
    }
    
    private void showShowTablePage(HttpServletRequest request, HttpServletResponse response, HttpSession session, ServletContext sc){
        System.err.println("----showShowTablePage()----");
        RequestDispatcher rd = sc.getRequestDispatcher("/show_table.jsp");
        try{
            rd.forward(request, response);
        }
        catch(ServletException e){
            System.err.println(e.getMessage());
        }
        catch(IOException e){
            System.err.println(e.getMessage());
        }
    }

    private void showShowTablePage2(HttpServletRequest request, HttpServletResponse response, HttpSession session, ServletContext sc){
        System.err.println("----showShowTablePage2()----");
        ArrayList<String> result = null;
        try {
            String tablename = request.getParameter("tablename");
            String colname = null;
            String cond = null;
            if(request.getParameter("colname") != ""){
                colname = request.getParameter("colname");
                cond = request.getParameter("cond");
            }
            result = dbhand.getData(tablename, colname, cond);
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
        if(result == null){
            System.out.println("result is null");
        }
        session.setAttribute("result", result);
        RequestDispatcher rd = sc.getRequestDispatcher("/show_table2.jsp");
        try{
            rd.forward(request, response);
        }
        catch(ServletException e){
            System.err.println(e.getMessage());
        }
        catch(IOException e){
            System.err.println(e.getMessage());
        }
    }
}


